﻿using Aker.DevTeam;
using Aker.Properties;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using System;
using System.Data;

namespace Aker.Uretim
{
    public partial class IadeOnay : XForm
    {
        public IadeOnay()
        {
            InitializeComponent();
        }

        private void IadeOnay_Load(object sender, EventArgs e)
        {
            DataTable dt = Data.DM.ExecuteDataTable("GetIade");
            gv.Init(dt);
            gv.RowCellStyle += gv_RowCellStyle;
            gv.SetCaptions("POrderIadeId:İade No", "IadeQty:Miktar", "Aciklama:Açıklama");
            gv.FormatAsNumber("IadeQty");
            GridMenu gm = GridManager.GetManager(gv).GridMenu;
            gm.AddMenu("Onayla", Onayla, Resources.Tik_32);
            gm.AddMenu("Reddet", Reddet, Resources.Remove);
            gv.OptionsView.ShowViewCaption = false;
            gv.BestFitColumns();

        }

        private void Reddet(object sender, EventArgs e)
        {
            UpdIade(2);
        }

        private void UpdIade(int type)
        {
            DataRow dr = gv.GetFocusedDataRow();
            int id = dr["POrderIadeId"].ToIntNN();
            Data.DM.ExecuteNonQuery("UpdIade", id, AppSession.UserInfo.UserId, type);
            gv.SetFocusedRowCellValue("Onaylayan", AppSession.UserInfo.Name);
            gv.SetFocusedRowCellValue("OnaylanmaTarihi", DateTime.Now);

            string aciklama = dr["Aciklama"].ToStringNN();
            if (type == 1)
                aciklama = "ONAY/" + aciklama.Replace("ONAY/", "").Replace("RED/", "");
            else
                aciklama = "RED/" + aciklama.Replace("ONAY/", "").Replace("RED/", "");

            gv.SetFocusedRowCellValue("Aciklama", aciklama);
            gv.BestFitColumns();
        }

        private void Onayla(object sender, EventArgs e)
        {
            UpdIade(1);
        }

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "Onaylayan" && e.RowHandle > -1)
            {
                if (e.CellValue.ToStringNN() == "")
                {
                    e.Appearance.BackColor = System.Drawing.Color.Red;
                }
            }
        }
    }
}
